#include "stdafx.h"
#include "../include/adopool.h"

#ifdef _DEBUG
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#undef DEBUG_NEW
	#define	DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
	#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
ADOPOOL_API long read_long(_variant_t&vtValue)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY) return(0);
	return((long)vtValue);
}

ADOPOOL_API BOOL read_string(_variant_t&vtValue,LPTSTR lpString,long sizeString)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY)
	{
		*lpString = 0;
		return(FALSE);
	}
	lstrcpyn(lpString,(_bstr_t)vtValue,sizeString);
	return(TRUE);
}

ADOPOOL_API BOOL read_stringA(_variant_t&vtValue,LPSTR lpString,long sizeString)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY)
	{
		*lpString = 0;
		return(FALSE);
	}
	lstrcpynA(lpString,(_bstr_t)vtValue,sizeString);
	return(TRUE);
}

ADOPOOL_API double read_double(_variant_t&vtValue)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY) return(0);
	return((double)vtValue);
}

ADOPOOL_API __int64 read_bigint(_variant_t&vtValue)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY) return(0);
	if(vtValue.vt==(VT_UI1|VT_ARRAY))
	{
		__int64 liData(0);
		BYTE tmp[8]={0},*p=NULL;
		SafeArrayAccessData(vtValue.parray,(void**)&p);
		memcpy(tmp,p,8);
		SafeArrayUnaccessData(vtValue.parray);
		p=(BYTE*)&liData;
		for(long i=8;i;)
		{
			*p++=tmp[--i];
		}
		return(liData);
	}
	DECIMAL decData=(DECIMAL)vtValue;
	__int64 liData=decData.Lo64;
	if(decData.sign==DECIMAL_NEG||decData.sign==-1) liData*=-1;
	return(liData);
}

ADOPOOL_API __int64 read_bigint_from_double(_variant_t&vtValue,long seek)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY) return(0);
	return((__int64)(((double)vtValue)*seek+0.5));
}

ADOPOOL_API long read_long_from_double(_variant_t&vtValue,long seek)
{
	if(vtValue.vt==VT_NULL||vtValue.vt==VT_EMPTY) return(0);
	return((long)(((double)vtValue)*seek+0.5));
}
